package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.persistence.mapper.UserShareMapper;
import com.zyd.blog.personal.service.UserProductionService;
import com.zyd.blog.personal.service.UserShareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户分享信息业务逻辑层
 * @author mfy
 */
@Service
public class UserShareServiceImpl implements UserShareService {

    @Resource
    private UserShareMapper userShareMapper;

    @Resource
    private UserProductionService userProductionService;

    public Map<String,Object> getCondition(Long id,Long userId,Long productionId,String status){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("id",id);
        condition.put("userId",userId);
        condition.put("productionId",productionId);
        condition.put("status", status);
        return condition;
    }

    @Override
    public Boolean isHaveShare(Long id,Long userId,Long productionId,String status){
        Map<String,Object> condition = getCondition(id,userId,productionId,status);
        List<Map<String,Object>> shareInfo = userShareMapper.queryUserShareByCondition(condition);
        return shareInfo != null && shareInfo.size() != 0;
    }

    @Override
    public List<Map<String,Object>> getShareByUserId(Long id,Long userId,Long productionId,String status){
        List<Map<String,Object>> result = new ArrayList<>();
        Map<String,Object> condition = getCondition(id,userId,productionId,status);
        List<Map<String,Object>> shareInfo = userShareMapper.queryUserShareByCondition(condition);
        for(Map<String,Object> item : shareInfo){
            Map<String,Object> map = new HashMap<>(16);
            map.put("shareId",item.get("id"));
            map.put("shareTime",item.get("updateTime"));
            Long productionIdC = (Long)item.get("productionId");
            List<Map<String,Object>> productionInfos = userProductionService.getUserProduction(null,productionIdC,null,Status.STATUS_YES, IsDelete.IS_DELETE_NO);
            if(productionInfos == null || productionInfos.size() == 0){
                map.put("productionIsDelete", IsDelete.IS_DELETE_YES);
                map.put("msg","作品已被删除");
            }else{
                map.put("productionIsDelete",IsDelete.IS_DELETE_NO);
                map.put("publishTime",productionInfos.get(0).get("publishTime"));
                map.put("coverPicture",productionInfos.get(0).get("coverPicture"));
                map.put("productionId",productionInfos.get(0).get("productionId"));
                map.put("label",productionInfos.get(0).get("label"));
                map.put("title",productionInfos.get(0).get("title"));
                map.put("content",productionInfos.get(0).get("content"));
            }
            result.add(map);
        }
        return result;
    }

    @Override
    public Object shareProduction(Long id,Long userId,Long productionId,String status){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("userId",userId);
        condition.put("productionId",productionId);
        switch (status){
            case Status.STATUS_YES:
                condition.put("updateTime",(new Date()).getTime());
                condition.put("addTime",(new Date()).getTime());
                return userShareMapper.addUserShare(condition) > 0;
            case Status.STATUS_NO:
                List<Map<String,Object>> shareInfo = getShareByUserId(id,userId,productionId,Status.STATUS_DEFAULT);
                if(shareInfo == null || shareInfo.size() == 0){
                    return false;
                }else {
                    condition.put("id",id);
                    condition.put("status", Status.STATUS_NO);
                    condition.put("updateTime",(new Date()).getTime());
                    return userShareMapper.updateUserShare(condition) > 0;
                }
            default:
                break;
        }
        return false;
    }

}
